/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.zx.course.study.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.mybatis.mapper.query.QueryType;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.entity.Employee;
import com.jeesite.modules.sys.entity.Office;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.EmpUserService;
import com.jeesite.modules.sys.service.OfficeService;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.modules.zx.course.study.entity.ZxCourseStudy;
import com.jeesite.modules.zx.course.study.service.ZxCourseStudyService;

import java.util.ArrayList;
import java.util.List;

/**
 * 课程学习表Controller
 * @author zpp
 * @version 2020-02-11
 */
@Controller
@RequestMapping(value = "${adminPath}/zx/course/study/zxCourseStudy")
public class ZxCourseStudyController extends BaseController {

	@Autowired
	private ZxCourseStudyService zxCourseStudyService;
	@Autowired
	private EmpUserService empUserService;
	@Autowired
	private OfficeService officeService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ZxCourseStudy get(String id, boolean isNewRecord) {
		return zxCourseStudyService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("zx:course:study:zxCourseStudy:view")
	@RequestMapping(value = {"list", ""})
	public String list(ZxCourseStudy zxCourseStudy, Model model) {
		Office office=new Office();
		office.setParentCode("0");
		List<Office> findList = officeService.findList(office);
		model.addAttribute("firstOfficeList",findList);
		model.addAttribute("secondOfficeList",new ArrayList<Office>());
		model.addAttribute("zxCourseStudy", zxCourseStudy);
		return "modules/zx/course/study/zxCourseStudyList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("zx:course:study:zxCourseStudy:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ZxCourseStudy> listData(ZxCourseStudy zxCourseStudy, HttpServletRequest request, HttpServletResponse response) {
		String firstOffice = zxCourseStudy.getFirstOffice();
		String secondOffice = zxCourseStudy.getSecondOffice();
		if(StringUtils.isNotBlank(firstOffice)) {
			EmpUser empUser=new EmpUser();
			empUser.getEmployee().getOffice().setIsQueryChildren(true);
			empUser.getEmployee().getCompany().setIsQueryChildren(true);
			
			if(StringUtils.isNotBlank(secondOffice)) {
				empUser.getEmployee().getOffice().setOfficeCode(secondOffice);
			}else {
				empUser.getEmployee().getOffice().setOfficeCode(firstOffice);
			}
			List<EmpUser> findList = empUserService.findList(empUser);
			List<String> userList=new ArrayList<String>();
			for(EmpUser user:findList) {
				String userCode = user.getUserCode();
				userList.add(userCode);
			}
			if(userList.size()>0) {
				zxCourseStudy.getSqlMap().getWhere().and("user_id", QueryType.IN, userList);
			}
		}
		zxCourseStudy.setPage(new Page<>(request, response));
		Page<ZxCourseStudy> page = zxCourseStudyService.findPage(zxCourseStudy);

		List<ZxCourseStudy> studyList = page.getList();
		if (studyList.size()>0){
			studyList.forEach(item->{
				Double duration = item.getDuration();
                if (duration!=null && duration!=0){
					item.setDuration((double) Math.round(duration /60));
			   }
				String userId = item.getUserId();
				if(StringUtils.isNotBlank(userId)) {
				  EmpUser empUser = empUserService.get(userId);
				  if(empUser!=null) {
					Employee employee = empUser.getEmployee();
					if(employee!=null) {
						Office office = employee.getOffice();
						if(office!=null) {
							item.setOfficeName(office.getOfficeName());
							String treeNames = office.getTreeNames();
							if(StringUtils.isNotBlank(treeNames)) {
								String[] split = treeNames.split("/");
								if(split.length>0) {
									item.setFirstOffice(split[0]);
									if(split[0].equals("其他机构")) {
										item.setOfficeName(empUser.getInputOffices());
									}
								}
								if(split.length>1) {
									item.setSecondOffice(split[1]);
								}
								
							}
						}
					}
					
				  }
				}
				
			});
		}
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("zx:course:study:zxCourseStudy:view")
	@RequestMapping(value = "form")
	public String form(ZxCourseStudy zxCourseStudy, Model model) {
		model.addAttribute("zxCourseStudy", zxCourseStudy);
		return "modules/zx/course/study/zxCourseStudyForm";
	}

	/**
	 * 保存课程学习表
	 */
	@RequiresPermissions("zx:course:study:zxCourseStudy:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ZxCourseStudy zxCourseStudy) {
		zxCourseStudyService.save(zxCourseStudy);
		return renderResult(Global.TRUE, text("保存课程学习表成功！"));
	}
	
	/**
	 * 停用课程学习表
	 */
	@RequiresPermissions("zx:course:study:zxCourseStudy:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(ZxCourseStudy zxCourseStudy) {
		zxCourseStudy.setStatus(ZxCourseStudy.STATUS_DISABLE);
		zxCourseStudyService.updateStatus(zxCourseStudy);
		return renderResult(Global.TRUE, text("停用课程学习表成功"));
	}
	
	/**
	 * 启用课程学习表
	 */
	@RequiresPermissions("zx:course:study:zxCourseStudy:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(ZxCourseStudy zxCourseStudy) {
		zxCourseStudy.setStatus(ZxCourseStudy.STATUS_NORMAL);
		zxCourseStudyService.updateStatus(zxCourseStudy);
		return renderResult(Global.TRUE, text("启用课程学习表成功"));
	}
	
	/**
	 * 删除课程学习表
	 */
	@RequiresPermissions("zx:course:study:zxCourseStudy:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ZxCourseStudy zxCourseStudy) {
		zxCourseStudyService.delete(zxCourseStudy);
		return renderResult(Global.TRUE, text("删除课程学习表成功！"));
	}
	/**
	 * 数据导出
	 */
	@RequestMapping(value = "exportData")
	public void exportData(ZxCourseStudy zxCourseStudy,HttpServletRequest request,HttpServletResponse response) {
		List<ZxCourseStudy> findList = zxCourseStudyService.findList(zxCourseStudy);
		for(ZxCourseStudy zcs:findList) {
			String userId=zcs.getUserId();
			EmpUser empUser = empUserService.get(userId);
			if(empUser==null) {
				continue;
			}
			Employee employee = empUser.getEmployee();
			if(employee!=null) {
				Office office = employee.getOffice();
				if(office!=null) {
					zcs.setOfficeName(office.getOfficeName());
					String treeNames = office.getTreeNames();
					if(StringUtils.isNotBlank(treeNames)) {
						String[] split = treeNames.split("/");
						if(split.length>0) {
							zcs.setFirstOffice(split[0]);
							if(split[0].equals("其他机构")) {
								zcs.setOfficeName(empUser.getInputOffices());
							}
						}
						if(split.length>1) {
							zcs.setSecondOffice(split[1]);
						}
					}
				}
			}
		}
		String fileName="用户学习统计"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
		try(ExcelExport ee=new ExcelExport("用户学习统计", ZxCourseStudy.class)) {
			ee.setDataList(findList).write(response,fileName);
		}
	}
}